package com.hospital.login.impl;

import com.alibaba.fastjson.JSON;
import com.hospital.login.dao.AdminUserMapper;
import com.hospital.login.entity.AdminUser;
import com.hospital.login.entity.AdminUserExample;
import com.hospital.login.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    AdminUserMapper adminUserMapper;

    @Override
    public Integer loginJudge(String num, String pwd) {
        //  根据num查出当前用户信息
        AdminUserExample example = new AdminUserExample();
        AdminUserExample.Criteria criteria = example.createCriteria();
        criteria.andAdNumEqualTo(num);
        List<AdminUser> adminUsers = adminUserMapper.selectByExample(example);
        if (adminUsers!=null){
            String pwdStr = adminUsers.get(0).getAdPassword();
            if (pwd.equals(pwdStr)){
                return 1;
            }
        }
        return 0;
    }

    @Override
    public String getAll() {
        AdminUserExample example = new AdminUserExample();
//        return JSON.toJSONString(adminUserMapper.selectByExample(example));
        List<AdminUser> adminUsers = adminUserMapper.selectByExample(example);
        //userNum
        //userName
        List<Map<String,String>> resultMap = new ArrayList<>();
        Map<String,String> map;
        for (AdminUser user : adminUsers) {
            map = new HashMap<>();
            map.put("userNum",user.getAdNum());
            map.put("userName",user.getAdName());
            resultMap.add(map);
        }
        return JSON.toJSONString(resultMap);
    }
}
